﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FDEEPublicFunc
{
    public static class BaseShapePloter
    {
        /// <summary>
        /// 绘制四边形
        /// </summary>
        /// <param name="upcenter"></param>
        /// <param name="wl"></param>
        /// <param name="wr"></param>
        /// <param name="h"></param>
        /// <returns></returns>
        public static Polyline AddPloy4(Point2d upcenter, double wl, double wr, double h)
        {
            Point2d ptop, pbot;
            Point2d p11, p12, p13, p14;
            ptop = upcenter;
            pbot = upcenter.Convert2D(0, -h);
            p11 = ptop.Convert2D(-wl);
            p12 = pbot.Convert2D(-wl);
            p13 = ptop.Convert2D(wr);
            p14 = pbot.Convert2D(wr);
            Polyline PL1 = new Polyline() { Closed = true };
            //PL1.AddVertexAt(0, ptop, 0, 0, 0);
            //PL1.AddVertexAt(1, p11, 0, 0, 0);
            //PL1.AddVertexAt(2, p12, 0, 0, 0);
            //PL1.AddVertexAt(3, pbot, 0, 0, 0);
            //PL1.AddVertexAt(4, p14, 0, 0, 0);
            //PL1.AddVertexAt(5, p13, 0, 0, 0);
            PL1.AddVertexAt(0, p12, 0, 0, 0);
            PL1.AddVertexAt(1, pbot, 0, 0, 0);
            PL1.AddVertexAt(2, p14, 0, 0, 0);

            PL1.AddVertexAt(3, p13, 0, 0, 0);
            PL1.AddVertexAt(4, ptop, 0, 0, 0);
            PL1.AddVertexAt(5, p11, 0, 0, 0);

            return PL1;
        }

        // 绘制任意图形
        public static void RenderEntity(Database db, Entity obj)
        {
            using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                recorder.AppendEntity(obj);
                tr.AddNewlyCreatedDBObject(obj, true);
                tr.Commit();

            }
            
        }

    }
}
